package com.isoft.student.controller;

import com.isoft.student.eo.TestpaperEOPage;
import com.isoft.student.model.Testpaper;
import com.isoft.student.service.TestpaperService;
import com.isoft.student.tool.ResponseMessage;
import com.isoft.student.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 对外提供数据接口
 */
@Api(tags = "testpaper")
@RestController
public class TestpaperController {

    @Autowired
    private TestpaperService testpaperService;

    @ApiOperation(value = "试卷添加方法")
    @RequestMapping(value = "/addTestpaper",method = RequestMethod.GET)
    public ResponseMessage add(Testpaper testpaper){
        int IsAdd = 0;
        //传入数据的判断
        //1.实体对象不能为空
        if (testpaper!=null){
            //2.试卷名称不能为空
            if (testpaper.getPapername()!=null){
                //3.添加
                IsAdd=testpaperService.AddTestpaper(testpaper);
                if (IsAdd == 1) {
                    return Result.success("保存成功");
                } else if (IsAdd == -1) {
                    return Result.error("角色重复不允许添加");
                } else {
                    return Result.error("保存失败");
                }
            }
        }
        return Result.error("保存失败");
    }

    @ApiOperation(value = "试卷修改方法")
    @RequestMapping(value = "/updateTestpaper",method = RequestMethod.GET)
    public ResponseMessage updateTestpaper(Testpaper testpaper){
        //传入的参数不能为空
        if (testpaper!=null){
            //参数当中主键不能为0
            if (testpaper.getTestpaperid()!=0){
                if (testpaperService.UpdateTestpaper(testpaper)==1){
                    if (testpaper.getPapername()!=null){
                        if (testpaper.getClassname()!=null){

                        }
                    }
                    return Result.success("更新成功");
                }else {
                    return Result.error("更新失败");
                }
            }
        }
        return Result.error("传入的参数不正确");
    }

    @ApiOperation(value = "试卷删除方法")
    @RequestMapping(value = "/delTestpaper",method = RequestMethod.GET)
    public ResponseMessage delTestpaper(String classname){
        if (classname!=null){
            int delInt=0;
            delInt=testpaperService.DeleteTestpaper(classname);
            if (delInt>0){
                return Result.success("删除成功");
            }else{
                return Result.error("删除失败");
            }
        }else{
            return Result.error("删除的主键不能为0，请重新输入");
        }
    }

    @ApiOperation(value = "根据条件获取试卷信息")
    @RequestMapping(value = "/getTestpaperWhereAll",produces = "application/json;charset=utf-8",method=RequestMethod.PUT)
    private ResponseMessage getAll(TestpaperEOPage testpaperEOPage){
        Map<String, List> map=new HashMap<>();
        List<TestpaperEOPage> listTestpaper=testpaperService.getTestpaperWhereAll(testpaperEOPage);
        map.put("list",listTestpaper);
        return Result.success("200",map);
    }
}
